#!/usr/bin/env python
# -*- coding:utf-8 -*-

import math
import json

from app import db
from ..models.userModel import Student
from ..utils import commons
from ..utils.loggings import loggings
from ..utils.response_code import RET, error_map_EN


class  user_sex(object):

    @classmethod
    def joint_query(cls, **kwargs):
    
        page = int(kwargs.get('Page', 1))
        size = int(kwargs.get('Size', 10))

        filter_list = []
    
        if kwargs.get('userID'):
            filter_list.append(Student.columns['userID']== kwargs.get('userID'))
        if kwargs.get('sex'):
            filter_list.append(Student.columns['sex']== kwargs.get('sex'))
        if kwargs.get('name'):
            filter_list.append(Student.columns['name'].like("%" + kwargs.get('name') + "%"))

        try:
            vuser_sexa = db.session.query(Student).filter(*filter_list)

            count = Student.count()
            pages = math.ceil(count / size)
            vuser_department_info = Student.limit(size).offset((page - 1) * size).all()

            a = ['userID','sex','name','departmentID','departmentName','seniority']
            b = dict(zip(a,list(vuser_department_info[0])))
            results = json.dumps(b)

            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'totalCount': count, 'totalPage': pages, 'data': results}
    
        except Exception as e:
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': str(e)}}
            
        finally:
            db.session.close()
